package com.bdqn.mr.partition;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 要想让改类的的对象能在网络中传输，
 * 就需要对类进行序列化
 */
// 第一步：实现Writable
public class FlowBean
        implements Writable
{
    private Long upFlow;    // 上行流量
    private Long downFlow;  // 下行流量
    private Long totalFlow; // 总流量


    // 第三步：给类添加一个无参的构造函数
    public FlowBean() {

    }

    public FlowBean(Long upFlow, Long downFlow) {
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.totalFlow = this.upFlow + this.downFlow;
    }

    public void setUpFlow(Long upFlow) {
        this.upFlow = upFlow;
    }

    public void setDownFlow(Long downFlow) {
        this.downFlow = downFlow;
    }

    public void setTotalFlow() {
        this.totalFlow = this.upFlow + this.downFlow;
    }

    // Getter方法没有写，Reducer中会用

    public Long getUpFlow() {
        return upFlow;
    }

    public Long getDownFlow() {
        return downFlow;
    }

    // 重写toString方法

    @Override
    public String toString() {
        return upFlow +
                "\t" + downFlow +
                "\t" + totalFlow ;
    }


    // 第二步：重写里面的抽象方法（包括序列化和反序列化方法）

    /**
     * 序列化方法
     *
     * @param out
     * @throws IOException
     */
    @Override
    public void write(DataOutput out) throws IOException {
        // 按照upFlow -》 downFlow -》 totalFlow顺序来的
        out.writeLong(this.upFlow);
        out.writeLong(this.downFlow);
        out.writeLong(this.totalFlow);
    }

    /**
     * 反序列化方法
     *
     * @param in
     * @throws IOException
     */
    @Override
    public void readFields(DataInput in) throws IOException {
        // 按照upFlow -》 downFlow -》 totalFlow顺序来的
        this.upFlow = in.readLong();
        this.downFlow = in.readLong();
        this.totalFlow = in.readLong();
    }


}
